package com.example.music.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.music.R;
import com.example.music.model.Song;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;

public class AddSongsAdapter extends RecyclerView.Adapter<AddSongsAdapter.ViewHolder> {

    private List<Song> originalList;
    private List<Song> filteredList;
    private Set<Song> selectedSongs = new HashSet<>();
    private OnSongSelectedListener listener;

    public interface OnSongSelectedListener {
        void onSongSelected(Song song, boolean isSelected);
    }

    public AddSongsAdapter(List<Song> songList, OnSongSelectedListener listener) {
        this.originalList = songList;
        this.filteredList = new ArrayList<>(songList);
        this.listener = listener;
    }

    public void filter(String query) {
        filteredList.clear();

        if (query == null || query.isEmpty()) {
            filteredList.addAll(originalList);
        } else {
            String lowerCaseQuery = query.toLowerCase();
            for (Song song : originalList) {
                if (song.getTitle().toLowerCase().contains(lowerCaseQuery)) {
                    filteredList.add(song);
                }
            }
        }

        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_add_song, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Song song = filteredList.get(position);
        holder.textSongName.setText(song.getTitle());
        holder.textArtist.setText(song.getArtist());

        // 设置选中状态
        holder.checkBox.setChecked(selectedSongs.contains(song));

        holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                selectedSongs.add(song);
            } else {
                selectedSongs.remove(song);
            }
            if (listener != null) {
                listener.onSongSelected(song, isChecked);
            }
        });

        holder.itemView.setOnClickListener(v -> {
            holder.checkBox.setChecked(!holder.checkBox.isChecked());
        });
    }

    @Override
    public int getItemCount() {
        return filteredList.size();
    }

    public List<Song> getSelectedSongs() {
        return new ArrayList<>(selectedSongs);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textSongName;
        TextView textArtist;
        CheckBox checkBox;

        ViewHolder(View itemView) {
            super(itemView);
            textSongName = itemView.findViewById(R.id.text_song_name);
            textArtist = itemView.findViewById(R.id.text_artist);
            checkBox = itemView.findViewById(R.id.checkbox);
        }
    }
}



